package com.basic.business.service.impl;

import com.basic.business.model.entity.SysRole;
import com.basic.business.model.dto.SysRoleDto;
import com.basic.business.model.vo.SysRoleVo;
import com.basic.business.mapper.SysRoleMapper;
import com.basic.business.service.SysRoleService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.*;

import java.util.*;

import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

/**
 * 角色表(SysRole)表服务实现类
 *
 * @author huage
 * @since 2021-07-20 14:33:46
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysRoleVo findById(Integer id) {
        SysRole sysRole = this.sysRoleMapper.selectById(id);

        //将SysRole转成SysRoleVo
        SysRoleVo sysRoleVo = null;
        if (sysRole != null) {
            sysRoleVo = new SysRoleVo();
            BeanUtils.copyProperties(sysRole, sysRoleVo);
        }

        return sysRoleVo;
    }

    /**
     * 查询多条数据
     *
     * @param sysRoleDto 实例对象
     * @param pageNum    页数
     * @param pageSize   每页条数
     * @return 对象列表
     */
    @Override
    public PageInfo<SysRoleVo> findPageList(SysRoleDto sysRoleDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        //将SysRole转成SysRoleVo
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDto, sysRole);
        List<SysRole> sysRoleList = this.sysRoleMapper.selectList(sysRole);

        PageInfo<SysRoleVo> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(new PageInfo<>(sysRoleList), pageInfo);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param sysRoleDto 实例对象
     * @return 实例对象
     */
    @Override
    public SysRoleVo add(SysRoleDto sysRoleDto) {
        //将SysRoleDto转成SysRole
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDto, sysRole);
        this.sysRoleMapper.insert(sysRole);

        return this.findById(sysRole.getId());
    }

    /**
     * 修改数据
     *
     * @param sysRoleDto 实例对象
     * @return 实例对象
     */
    @Override
    public SysRoleVo modify(SysRoleDto sysRoleDto) {
        //将SysRoleDto转成SysRole
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDto, sysRole);
        this.sysRoleMapper.update(sysRole);

        return this.findById(sysRole.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean remove(Integer id) {
        return this.sysRoleMapper.deleteById(id) > 0;
    }

    @Override
    public List<SysRole> findByLoginId(Integer loginId) {
        return sysRoleMapper.findByLoginId(loginId);
    }
}

